#include "qdateintervalwidget.h"
#include <QDebug>
#include <QDateTime>
#include <QPalette>
#include <QTextCharFormat>
#include <QPainter>
#include <QCalendarWidget>

QDateIntervalWidget::QDateIntervalWidget(QWidget *parent)
    : QWidget(parent)
{
    this->resize(718, 389);
    this->setWindowFlags(Qt::FramelessWindowHint);
     m_outlinePen.setColor(Qt::red);
     m_transparentBrush.setColor(Qt::transparent);

    /* Init sub widgets */
    label = new QLabel("至");
    calendar_start = new QCalendarWidget();
    calendar_end = new QCalendarWidget();
    btn_today = new QPushButton("今天");
    btn_10day = new QPushButton("近十天");
    btn_month = new QPushButton("近一月");
    btn_year = new QPushButton("近一年");
    btn_confirm = new QPushButton("确定");
    btn_cancel = new QPushButton("取消");

    /* Layout */
    // Top layout
    hLayout_BtnDate = new QHBoxLayout();
    hLayout_BtnDate->addStretch();
    hLayout_BtnDate->addWidget(btn_today);
    hLayout_BtnDate->addWidget(btn_10day);
    hLayout_BtnDate->addWidget(btn_month);
    hLayout_BtnDate->addWidget(btn_year);
    // Middle layout
    hLayout_calendar = new QHBoxLayout();
    hLayout_calendar->addWidget(calendar_start);
    hLayout_calendar->addWidget(label);
    hLayout_calendar->addWidget(calendar_end);
    // Bottom layout
    hLayout_OkCancel = new QHBoxLayout();
    hLayout_OkCancel->addStretch();
    hLayout_OkCancel->addWidget(btn_confirm);
    hLayout_OkCancel->addWidget(btn_cancel);
    // Main layout
    vMainLayout = new QVBoxLayout();
    vMainLayout->addLayout(hLayout_BtnDate);
    vMainLayout->addLayout(hLayout_calendar);
    vMainLayout->addLayout(hLayout_OkCancel);
    this->setLayout(vMainLayout);

    /* connect */
    connect(btn_today, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_today_clicked);
    connect(btn_10day, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_10day_clicked);
    connect(btn_month, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_month_clicked);
    connect(btn_year, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_year_clicked);
    connect(btn_confirm, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_confirm_clicked);
    connect(btn_cancel, &QPushButton::clicked, this, &QDateIntervalWidget::on_btn_cancel_clicked);

    /* Cut off VerticalHeader */
    this->calendar_start->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    this->calendar_end->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);;

    /* Init Calendar_start widget to last month */
     this->calendar_start->setSelectedDate(QDate::currentDate().addMonths(-1));

    /* Init the maximum date to today */
     this->calendar_end->setMaximumDate(QDate::currentDate());
    QTextCharFormat format;
    format.setForeground(QColor(160, 160, 160));
    format.setBackground(QColor(255, 255, 255));
    this->calendar_end->setDateTextFormat(QDate::currentDate(), format);
//    initSetting();
//    this->date_end = QDate::currentDate();
//    QTextCharFormat format;
//    format.setBackground(Qt::red);
//    this->calendar_end->setDateTextFormat(this->date_end, format);
}

QDateIntervalWidget::~QDateIntervalWidget()
{
}

void QDateIntervalWidget::initSetting()
{
    setLocale(QLocale(QLocale::Chinese));
    this->calendar_start->setSelectionMode(QCalendarWidget::SingleSelection);
    this->calendar_start->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    this->calendar_start->setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);

    QTextCharFormat format;
    format.setForeground(QColor(160, 160, 160));
    format.setBackground(QColor(255, 255, 255));
    this->calendar_start->setHeaderTextFormat(format);
    this->calendar_start->setWeekdayTextFormat(Qt::Saturday, format);
    this->calendar_start->setWeekdayTextFormat(Qt::Sunday,   format);
    this->calendar_start->setWeekdayTextFormat(Qt::Monday,   format);
    this->calendar_start->setWeekdayTextFormat(Qt::Tuesday,  format);
    this->calendar_start->setWeekdayTextFormat(Qt::Wednesday,format);
    this->calendar_start->setWeekdayTextFormat(Qt::Thursday, format);
    this->calendar_start->setWeekdayTextFormat(Qt::Friday,   format);
}

void QDateIntervalWidget::on_btn_today_clicked()
{
    this->date_start = QDate::currentDate();
    this->date_end = QDate::currentDate();
//    this->calendar_end->paintCell
}
void QDateIntervalWidget::on_btn_10day_clicked()
{
    this->date_start = QDate::currentDate().addDays(-10);
    this->date_end = QDate::currentDate();
}
void QDateIntervalWidget::on_btn_month_clicked()
{
    this->date_start = QDate::currentDate().addMonths(-1);
    this->date_end = QDate::currentDate();
}
void QDateIntervalWidget::on_btn_year_clicked()
{
    this->date_start = QDate::currentDate().addYears(-1);
    this->date_end = QDate::currentDate();
}

void QDateIntervalWidget::on_btn_confirm_clicked()
{

}
void QDateIntervalWidget::on_btn_cancel_clicked()
{
    this->close();
}

void QDateIntervalWidget::on_calendar_end_clicked()
{
    // Check if calendar_start has selected?
    if(this->date_start == QDate::currentDate())
    {
//        date_end_flag
    }
}

